﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Web.Routing;

namespace HomeFramework
{
    public class Global : System.Web.HttpApplication
    {
        void Application_Start(object sender, EventArgs e)
        {
            //RouteTable.Routes.Add(
            //  new ServiceRoute("router", new CustomServiceHostFactory(),

            //      typeof(RoutingService)));

            Log.WriteLine(Log.SourceEnum.HomeFrameworkApplication, System.Diagnostics.TraceEventType.Information, "Application starting on {0}", Environment.MachineName);
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
            AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
        }

        System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
        {
            return AppDomain.CurrentDomain.GetAssemblies().SingleOrDefault(
                x => x.FullName == args.Name);
        }

        void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            Log.WriteLine(Log.SourceEnum.HomeFrameworkApplication, System.Diagnostics.TraceEventType.Critical, (e.ExceptionObject as Exception).Message);
        }

        void Application_End(object sender, EventArgs e)
        {
            Log.WriteLine(Log.SourceEnum.HomeFrameworkApplication, System.Diagnostics.TraceEventType.Information, "Application ending son {0}", Environment.MachineName);
        }

        void Application_Error(object sender, EventArgs e)
        {
            Log.WriteLine(Log.SourceEnum.HomeFrameworkApplication, System.Diagnostics.TraceEventType.Critical, HttpContext.Current.Server.GetLastError().Message);
        }

        void Session_Start(object sender, EventArgs e)
        {
            Log.WriteLine(Log.SourceEnum.HomeFrameworkApplication, System.Diagnostics.TraceEventType.Verbose, "Session starting");
        }

        void Session_End(object sender, EventArgs e)
        {
            Log.WriteLine(Log.SourceEnum.HomeFrameworkApplication, System.Diagnostics.TraceEventType.Verbose, "Session ending");
        }
    }
}
